package com.bbc.base;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;

import com.bbc.R;

/**
 * Created by Administrator on 2016/12/7 0007.
 * 所有片段的基类 ,,这个是ListViewFragment的基类。
 * 需要再进行讨论
 * 新基类
 */

public abstract class BaseRefreshFragment<T> extends Fragment {
    public ContentPager contentPager;
    //RecyclerView
    protected RecyclerView mRecyclerView;
    //SwipeRefreshLayout 可以供大家使用,刷新控件
    public SwipeRefreshLayout mSrfLayout;
    protected FragmentActivity mActivity;
    //适配器
    private BaseRecAdapter mAdapter;
    private LinearLayoutManager mLayoutManager;
    //最后一个可见项的位置，用于下拉刷新
    private int mLastVisibleItemPosition;

    //正在加载更多的判断位
    private boolean isLoadMore;
    //加载更多的View
    private View mLoadMoreView;
    //加载更多的TextView
    private TextView mTvLoadMore;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        mActivity = getActivity();
        if (contentPager == null) {
            contentPager = new ContentPager(mActivity) {
                //创建片段的成功界面,我们知道他们都是recyclerView
                @Override
                protected View onCreateSuccessView() {
                    /*mRecyclerView = new RecyclerView(getActivity());*/
                    mSrfLayout = (SwipeRefreshLayout) mActivity.getLayoutInflater().inflate(R.layout.fragment_list_base, container, false);
                    mRecyclerView = (RecyclerView) mSrfLayout.findViewById(R.id.rec_base);
                    return mSrfLayout;
                }
            };
        }
        //    -在低版本v4库中的FragmentManager，Fragment的界面视图会有一个父控件引用它，
        // 当重用Fragment的界面视图时，需先与父控件解除关系，再返回
        else {
            ViewParent parent = contentPager.getParent();
            //当有父控件，且是viewGroup的实例 ,移除ContentPager
            if (parent != null && parent instanceof ViewGroup) {
                ViewGroup viewGroup = (ViewGroup) parent;
                viewGroup.removeView(contentPager);
            }
        }
        return contentPager;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //这个方法会真正添加recView到控件中
        //添加完毕
        //可以去设置adapter，
        mAdapter = (BaseRecAdapter) getAdapter();
        if (mAdapter == null) {
            return;
        }
        //添加分割线
        mRecyclerView.addItemDecoration(new RecycleViewDivider(mActivity, LinearLayoutManager.HORIZONTAL));
        mRecyclerView.setAdapter(mAdapter);
        mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        //初始化事件，刷新相关的操作
        initEvent();
    }

    //因为数据不一样。所以需要子类去设置自己的adapter
    protected abstract RecyclerView.Adapter getAdapter();

    //提供一个更新数据状态的方法
    public void updateData(ResultState resultState) {
        contentPager.loadDataOrRefreshData(resultState);
    }


    // --------------------上拉加载更多以及下拉刷新的的相关操作


    private void initEvent() {
        //上拉刷新
        mSrfLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                doRefresh();
            }

        });
        //下拉加载更多
        mRecyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                //孩子的所有的数量-1= 最后一个可见项并且静止的时候，让加载更多可以显示
                if (mAdapter.getItemCount() - 1 == mLastVisibleItemPosition
                        && RecyclerView.SCROLL_STATE_IDLE == newState) {
                    //拿到加载更多的View
                    mLoadMoreView = mAdapter.getLoadMoreView();
                    //拿到加载更多的TextView
                    mTvLoadMore = mAdapter.getTvLoadMore();
                    //让他可见
                    if (mLoadMoreView != null) {
                        mLoadMoreView.setVisibility(View.VISIBLE);
                    }
                    //如果没有正在加载更多，并且如果监听器不为空
                    if (!isLoadMore && mOnRefreshListener != null) {
                        //判断位变成true以免疯狂刷新
                        isLoadMore = true;
                        //上拉加载更多
                        mOnRefreshListener.onUpLoadMore();
                    }/*else{
                        isLoadMore = false;  这个可以注释掉。
                    }*/
                }
            }


            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //当recyclerView正在滑动的时候，获取最后一个可见项目的ID
                mLastVisibleItemPosition = mLayoutManager.findLastVisibleItemPosition();
            }
        });
    }

    private OnRefreshListener mOnRefreshListener;

    /**
     * 刷新的监听器
     *
     * @param onRefreshListener
     */
    public void setOnRefreshListener(OnRefreshListener onRefreshListener) {
        mOnRefreshListener = onRefreshListener;
    }

    //刷新回调的接口
    public interface OnRefreshListener {
        //下拉刷新的回调

        /**
         * 可以在这个回调方法里面调用，loadMoreCompleted(),通知内部加载更多完成
         */
        void onUpLoadMore();
    }

    //提供一个方法,让外面通知已经刷新完成
    public void loadMoreCompleted() {
        isLoadMore = false;
        if (mLoadMoreView != null) {
            mLoadMoreView.setVisibility(View.GONE);
        }
    }

    public abstract void doRefresh();
    //提供一个方法，让外面通知已经没有更多数据了
    public void noMoreData() {
        isLoadMore = false;
        if (mTvLoadMore != null) {
            if (Global.isNetworkReachable(mActivity)) {
                mTvLoadMore.setVisibility(View.VISIBLE);
                mTvLoadMore.setText("没有更多数据了...");
            } else {
                mTvLoadMore.setText("你进入了没有网络的异次元");
            }
        }
    }
}

